﻿fdebugger = {
    _init: function () {
        //outerdiv
        var outerdiv = document.createElement("div");
        outerdiv.setAttribute("style", "border:1px solid #000;position:absolute;background-color:#fff;display:");
        outerdiv.setAttribute("id", this._id.main);

        //movediv
        var movediv = document.createElement("div");
        movediv.setAttribute("style", "background-color:gray;height:25px;");
        movediv.setAttribute("id", this._id.mover);
        outerdiv.appendChild(movediv);

        //content div
        var cntdiv = document.createElement("div");
        cntdiv.setAttribute("style", "padding:5px;");

        //inputdiv
        var inputdiv = document.createElement("div");
        inputdiv.setAttribute("style", "border:1px solid #000;display:none;");
        var inputbox = document.createElement("textarea");
        inputbox.setAttribute("id", this._id.inputer);
        inputbox.setAttribute("rows", 5);
        inputbox.setAttribute("cols", 43);
        inputbox.onkeypress = function (e) {
            var e = e || window.event;
            if (e.ctrlKey && e.keyCode == 13)
                fdebugger._run();
        };
        inputdiv.appendChild(inputbox);
        cntdiv.appendChild(inputdiv);

        //btndiv
        var btndiv = document.createElement("div");
        btndiv.setAttribute("style", "text-align:right;");

        var btn_run = document.createElement("input");
        btn_run.setAttribute("type", "button");
        btn_run.onclick = this._run;
        btn_run.setAttribute("value", "Run");
        btndiv.appendChild(btn_run);


        var btn_hide_up = document.createElement("input");
        btn_hide_up.setAttribute("type", "button");
        btn_hide_up.setAttribute("value", "Show/Hide↑");
        btn_hide_up.onclick = function () { fdebugger._showhide(document.getElementById(fdebugger._id.inputer).parentNode) };
        btndiv.appendChild(btn_hide_up);

        var btn_hide_down = document.createElement("input");
        btn_hide_down.setAttribute("type", "button");
        btn_hide_down.setAttribute("value", "Show/Hide↓");
        btn_hide_down.onclick = function () { fdebugger._showhide(document.getElementById(fdebugger._id.outer).parentNode) };
        btndiv.appendChild(btn_hide_down);

        var btn_clear_down = document.createElement("input");
        btn_clear_down.setAttribute("type", "button");
        btn_clear_down.setAttribute("value", "Clear↓");
        btn_clear_down.onclick = function () { document.getElementById(fdebugger._id.outer).value = ""; };
        btndiv.appendChild(btn_clear_down);

        var btn_close = document.createElement("input");
        btn_close.setAttribute("type", "button");
        btn_close.onclick = function () { fdebugger._showhide(document.getElementById(fdebugger._id.main)) };
        btn_close.setAttribute("value", "Close");
        btndiv.appendChild(btn_close);

        cntdiv.appendChild(btndiv);

        //outputdiv
        var outputdiv = document.createElement("div");
        outputdiv.setAttribute("style", "border:1px solid #000;display:none;");
        var outputbox = document.createElement("textarea");
        outputbox.setAttribute("id", this._id.outer);
        outputbox.setAttribute("rows", 20);
        outputbox.setAttribute("cols", 43);
        outputdiv.appendChild(outputbox);
        cntdiv.appendChild(outputdiv);

        outerdiv.appendChild(cntdiv);
        document.body.appendChild(outerdiv);
    },

    _showhide: function (obj) {
        if (obj.style.display == 'none')
            obj.style.display = '';
        else obj.style.display = 'none';
    },

    _id: {
        main: "fdb",
        mover: "fdb_mover",
        inputer: "fdb_inputer",
        outer: "fdb_outer"
    },

    _run: function () {
        var print = function (val) { document.getElementById(fdebugger._id.outer).value += val; }
        var println = function (val) { print(val + "\r"); }
        eval(document.getElementById(fdebugger._id.inputer).value);
    },

    start: function () {
        this._init();
        setMover(document.getElementById(this._id.mover), document.getElementById(this._id.main));
    }
}

function setMover(moveKey, moveArea) {
    moveKey.onmousedown = setMover.readyMove;
    moveKey.style.cursor = "move";
    setMover.moveKey = moveKey;
    setMover.moveArea = moveArea ? moveArea : moveKey;
}
setMover.readyMove = function (e) {
    var e = e || window.event;
    setMover.startPos = { left: e.clientX, top: e.clientY };
    var l = parseInt(setMover.moveArea.style.left) || 0;
    var r = parseInt(setMover.moveArea.style.top) || 0;
    setMover.originPos = { left: l, top: r };
    document.onmousemove = setMover.startMove;
    document.onmouseup = setMover.stopMove;
}
setMover.startMove = function (e) {
    var e = e || window.event;
    window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
    var sp = setMover.startPos;
    var op = setMover.originPos;
    var ma = setMover.moveArea;
    var lm = e.clientX - sp.left;
    var rm = e.clientY - sp.top;
    ma.style.left = op.left + lm + "px";
    ma.style.top = op.top + rm + "px";
}
setMover.stopMove = function () {
    document.onmousemove = null;
    document.onmouseup = null;
}